package com.jindun.paper.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jindun.common.Constants;
import com.jindun.paper.dao.SettingRepository;
import com.jindun.paper.model.Setting;
import com.jindun.paper.service.SettingService;

@Service
@Transactional
public class SettingServiceImpl implements SettingService {

	@Autowired
	private SettingRepository settingRepository;

	@Autowired
	private EntityManager entityManager;

	@Override
	public List<Integer> findAllLayers(String status) {
		return this.findIntValues(Setting.CATALOG_BAG_LAYER, status);
	}

	@Override
	public List<Integer> findAllStubSpecs(String status) {
		return this.findIntValues(Setting.CATALOG_STUB_SPEC, status);
	}

	private List<Integer> findIntValues(String catalog, String status) {
		List<Setting> settingList = this.settingRepository
				.findByCatalogAndStatusOrderByValue(catalog, status);
		List<Integer> valueList = new ArrayList<Integer>(settingList.size());
		for (Setting setting : settingList) {

			valueList.add(Integer.parseInt(setting.getValue()));

		}
		return valueList;
	}

	@Override
	public List<Setting> findAllPlaces(String status) {
		List<Setting> settingList = this.settingRepository
				.findByCatalogAndStatusOrderByValue(
						Setting.CATALOG_OFFLOAD_PLACE, status);
		return settingList;
	}

	@Override
	public List<Setting> findAllSourcePlaces(String status) {
		List<Setting> settingList = this.settingRepository
				.findByCatalogAndStatusOrderByValue(Setting.CATALOG_LOCATION,
						status);
		return settingList;
	}

	@Override
	public List<Setting> findAllPaymentMethods(String status) {
		List<Setting> settingList = this.settingRepository
				.findByCatalogAndStatusOrderByValue(
						Setting.CATALOG_PAYMENT_METHOD, status);
		return settingList;
	}

	@Override
	public List<Setting> findAll() {
		return this.settingRepository.findAll();
	}

	@Override
	public Setting getOne(Integer settingId) {
		Setting setting = this.settingRepository.getOne(settingId);
		Hibernate.initialize(setting);
		return setting;
	}

	@Override
	public List<Setting> findSettings(String catalog, String status) {
		return this.settingRepository.findByCatalogAndStatusOrderByValue(
				catalog, status);
	}

	@Override
	public boolean isUnique(Setting setting) {

		StringBuilder jql = new StringBuilder();
		jql.append("from Setting s where s.catalog = ?  and s.value = ?");
		if (setting.getId() != null) {
			jql.append(" and s.id != ?");
		}

		Query query = this.entityManager.createQuery(jql.toString());
		query.setParameter(1, setting.getCatalog());
		query.setParameter(2, setting.getValue());

		if (setting.getId() != null) {
			query.setParameter(3, setting.getId());
		}

		return query.getResultList().size() == 0;
	}

}
